<template>
  <el-form ref="form" v-loading="loading" :rules="rules" :model="form" label-width="100px">
    <el-form-item label="标题：" data="发布会员" prop="tittle">
      <el-input v-model="form.tittle" style="width:202px;" placeholder="标题" />
    </el-form-item>
    <el-form-item label="发布会员：" data="发布会员" prop="memberId">
      <el-select v-model="form.memberId" style="width:202px;" placeholder="发布会员">
        <el-option v-for="item in Arr" :key="item.id" :label="item.nickName" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item v-if="resultDics.INFORMATION_TOPIC" label="话题类型：" prop="topic">
      <el-select v-model="form.topic" style="width:202px;" placeholder="话题类型">
        <el-option
          v-for="item in resultDics.INFORMATION_TOPIC.children"
          :key="item.id"
          :label="item.dicName"
          :value="item.dicCode"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="资讯类型：" prop="type">
      <el-select v-model="form.type" style="width:202px;" placeholder="资讯类型">
        <el-option v-for="item in options" :key="item.val" :label="item.key" :value="item.val" />
      </el-select>
    </el-form-item>
    <el-form-item label="发布时间：" v-if="!form.status">
      <el-date-picker
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss"
        v-model="form.taskTime"
        type="datetime"
        placeholder="发布时间"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="是否发布：">
      <el-checkbox v-model="form.status" />
    </el-form-item>
    <el-form-item v-if="tagsArr.length" label="标签：">
      <NewTags :data="tagsArr" :init-data="initData" @changeTag="changeTag" />
    </el-form-item>
    <el-form-item label="资讯内容：">
      <Editor :content="textarea" show-btn="true" @submitText="submitText" />
    </el-form-item>
    <div style="display:flex;justify-content:center;">
      <el-button class="right" type="primary" @click="submitInfoMation">提 交</el-button>
    </div>
  </el-form>
</template>

<script>
import mmUrl from "~/utils/utilUrl";
import Editor from "../commom/Editor";
import { selfdicCode, createDics } from "~/utils/util";
import NewTags from "../commom/newTags";
import regular from "~/utils/regular";
export default {
  components: {
    Editor,
    NewTags
  },
  props: ["id"],
  data() {
    return {
      resultDics: {},
      dataId: this.$props.id === "create" ? "" : this.$props.id,
      isCreateText: true, // 是不是创建id
      isCreateInfo: true, // 是不是创建帖子
      loading: true,
      dics: [],
      Arr: [],
      initData: [],
      rules: {
        tittle: regular.require1,
        memberId: regular.requireChange,
        topic: regular.requireChange,
        type: regular.requireChange
      },
      tagsArr: [],
      options: [
        {
          key: "生意资讯",
          val: 0
        },
        {
          key: "生意圈",
          val: 1
        },
        {
          key: "线下沙龙",
          val: 2
        },
        {
          key: "新闻快讯",
          val: 3
        },
        {
          key: "系统信息",
          val: 4
        }
      ],
      form: {
        memberId: "",
        status: 0,
        topic: "",
        type: "",
        label: "",
        tittle: "",
        taskTime: ""
      },
      textarea: "", // 富文本内容
      typeCode: "",
      textId: "" // 富文本存在的ID
    };
  },
  mounted() {
    this.$props.id === "create"
      ? (this.isCreateInfo = true)
      : (this.isCreateInfo = false);
    this.loadInitDate();
    this.getInitLabel();
  },
  methods: {
    changeTag(res) {
      this.form.label = JSON.stringify(res);
    },
    submitText(res) {
      this.textarea = res.text;
    },
    // 点击提交按钮触发的函数
    submitInfoMation() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.status) {
            let timerArr = this.form.taskTime.split(":");
            timerArr[1] = ":00";
            timerArr[2] = ":00";
            this.form.taskTime = timerArr.join("");
          }
          if (this.form.status) {
            delete this.form.taskTime;
          }
          this.$post({
            url: this.isCreateInfo
              ? mmUrl.Infomation.create
              : mmUrl.Infomation.edit,
            data: {
              item: this.form
            }
          }).then(res => {
            this.submitTextInforMation(res.item.id, this);
            this.$emit("closeDialog");
          });
        }
      });
    },
    // 点击提交按钮触发的函数
    // 提交按钮要执行的回调函数
    submitTextInforMation(dataId) {
      const item = this.isCreateText
        ? {
            content: this.textarea,
            dataId,
            dataType: "资讯帖子详情",
            typeCode: this.typeCode
          }
        : {
            id: this.textId,
            content: this.textarea
          };
      this.submitText1({
        url: this.isCreateText ? mmUrl.text.create : mmUrl.text.editor,
        data: {
          item
        }
      }).then(res => {
        this.textarea = res;
        this.$message({
          type: "success",
          message: "操作成功"
        });
      });
    },
    // 提交按钮要执行的回调函数
    // 统一提交富文本的函数
    submitText1(res) {
      return new Promise(resolve => {
        this.$post({
          url: res.url,
          data: res.data
        })
          .then(res => {
            resolve(res.item.content);
          })
          .catch(err => this.$message.error(err));
      });
    },
    // 统一提交富文本的函数
    // 获取标签的接口
    getInitLabel() {
      this.$post({
        url: mmUrl.label.pageLoad,
        data: {
          keyword: "string",
          page: {
            maxResultCount: 0,
            pageNo: 0,
            pageSize: 10,
            skipCount: 0
          },
          type: 0
        }
      }).then(res => {
        this.tagsArr = res.items.items;
      });
    },
    // 获取标签的接口
    // 获取页面基本信息的接口
    loadInitDate() {
      const id = this.isCreateInfo ? "" : this.dataId;
      this.$post({
        url: this.isCreateInfo
          ? mmUrl.Infomation.loadCreate
          : mmUrl.Infomation.loadEdit,
        data: {
          id
        }
      }).then(res => {
        this.dics = res.dics;
        this.typeCode = selfdicCode({
          data: res.dics,
          dicCode: "TEXTINFORMATION_POSTINFORMATION"
        });
        this.resultDics = createDics(res.dics, "dicCode");
        this.Arr = res.memberDtos;
        this.loading = false;
        if (!this.isCreateInfo) {
          this.form = res.item;
          this.sureIsCreate(res.item.id);
          this.initData = JSON.parse(res.item.label);
        }
      });
    },
    // 获取页面基本信息的接口
    // 富文本$emit触发的函数
    changeIndustry(e) {
      this.form.topic = e;
    },
    // 富文本$emit触发的函数
    // 获取富文本内容
    sureIsCreate(id) {
      this.$post({
        url: mmUrl.text.get,
        data: {
          dataId: id,
          typeCode: this.typeCode
        }
      }).then(res => {
        if (!res.item) {
          return false;
        } else {
          this.isCreateText = false;
          this.textarea = res.item.content;
          this.textId = res.item.id;
        }
      });
    }
  }
};
</script>
<style>
</style>
